package com.gko100.action;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.gko100.action.base.AdminBaseAction;

import com.gko100.model.Category;
import com.gko100.model.User;
import com.opensymphony.xwork2.ActionContext;


public class UserAction extends AdminBaseAction {
	
	private static int pageSize=10;
	private int totalCount=0;
	private int pageNo;
	private int pageCount;

	
	public int getPageSize() {
		return pageSize;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	
	private String checkboxListDel;
	private long[]delUserId=new long[]{};
	
	public long[] getDelUserId() {
		return delUserId;
	}
	public void setDelUserId(long[] delUserId) {
		this.delUserId = delUserId;
	}
	public String getCheckboxListDel() {
		return checkboxListDel;
	}
	public void setCheckboxListDel(String checkboxListDel) {
		this.checkboxListDel = checkboxListDel;
	}
	
	
	
	private Category cate;
	private List<Category> cateList;

	public Category getCate() {
		return cate;
	}
	public void setCate(Category cate) {
		this.cate = cate;
	}
	public List<Category> getCateList() {
		return cateList;
	}
	public void setCateList(List<Category> cateList) {
		this.cateList = cateList;
	}

	private User user;
	private List<User> userList;
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public List<User> getUserList() {
		return userList;
	}
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}
	
	
	public String insert_user() throws Exception{
		
		if(this.getUser()!=null){
			this.setUser(null);
		}
		return "user_insert_success";
	}
	
	public String add_user() throws Exception {
		
		if(this.getUser()!=null){
			
			adminManager.add_user(this.getUser());
			adminManager.add_userPermByUser(this.getUser());
		}

		return "user_add_success";
	}
	private Map<Boolean, String> permStateMap;
	
	public Map<Boolean, String> getPermStateMap() {
		return permStateMap;
	}
	public void setPermStateMap(Map<Boolean, String> permStateMap) {
		this.permStateMap = permStateMap;
	}
	public String upload_user() throws Exception {
		
		if(this.getUser().getUserId()!=0){
			
			User u = adminManager.findUserById(this.getUser().getUserId());
			if(u!=null){
				this.setUser(u);
			}
			Map<Boolean, String> permStateMap = new LinkedHashMap<Boolean, String>();
			permStateMap.put(false, "未通过审核");
			permStateMap.put(true, "已通过审核");
			this.setPermStateMap(permStateMap);
			
		}

		return "user_upload_success";
	}
	
	public String modify_user() throws Exception {
		
		if(this.getUser()!=null){
			
			adminManager.modify_user(this.getUser());
		}
		
		return "user_modify_success";
	}
	
	private List<User> uvList;
	
	public List<User> getUvList() {
		return uvList;
	}
	public void setUvList(List<User> uvList) {
		this.uvList = uvList;
	}
	public String find_userList() throws Exception{
		
		if(this.getUserList()!=null){
			this.setUserList(null);
		}
		if(this.getTotalCount()!=0){
			this.setTotalCount(0);
		}

		List<User> ul = adminManager.findUserAll();
		if(ul!=null){
			this.setUvList(ul);

		}
		if(this.getUvList()!=null){
			
			this.setTotalCount(this.getUvList().size());
			int pageCount = this.getTotalCount()%this.getPageSize();
			if(pageCount!=0){
				this.setPageCount(this.getTotalCount()/this.getPageSize()+1);
			}else{
				this.setPageCount(this.getTotalCount()/this.getPageSize());
			}
		}
		if(this.getPageNo()<1){
			this.setPageNo(1);
		}
		if(this.getPageCount()==0){
			this.setPageNo(0);
		}

		if(this.getPageNo()==1){
			List<User> ult = new ArrayList<User>();
			if(this.getUvList()!=null){
				for(int i=0;i<this.getPageSize()&&i<this.getUvList().size();i++){
					User u = this.getUvList().get(i);
					if(u!=null){
						ult.add(u);
					}
				}
				this.setUserList(ult);
				
				if(ult!=null){
					ult=null;
				}
			}
			
		}
		if(this.getPageNo()>1&&this.getPageNo()<=this.getPageCount()){
			
			List<User> ult = new ArrayList<User>();
			int firstResult = (this.getPageNo()-1)*this.getPageSize();
			if(this.getUvList()!=null){
				for(int i=firstResult;i<firstResult+this.getPageSize()&&i<this.getUvList().size();i++){
					
					User u = this.getUvList().get(i);
					if(u!=null){
						ult.add(u);
					}
				}
				this.setUserList(ult);
				if(ult!=null){
					ult=null;
				}
			}
		}
		return "userList_find_success";
	}
	
	public String del_user() throws Exception {
		
		if(this.getUser().getUserId()!=null){
			
			User u = adminManager.findUserById(this.getUser().getUserId());
			if(u!=null){
				u.setDeleteFlag(true);
				adminManager.modify_user(u);
			}
		}
		
		return "user_del_success";
	}
	
	//彻底删除会员信息
	public String cdel_user() throws Exception {
		
		if(this.getUser().getUserId()!=null){
			
			User u = adminManager.findUserById(this.getUser().getUserId());
			if(u!=null){
				
				adminManager.del_user(u);
				
			}
		}
		
		
		return "user_cdel_success";
	}
	
	public String del_userList() throws Exception {
		
		String delId[] = this.getCheckboxListDel().split(", ");
		if(delId!=null&&delId.length>=1){
			for(int i=0;i<delId.length;i++){		
				
				User u = adminManager.findUserById(Integer.parseInt(delId[i]));
				if(u!=null){
					u.setDeleteFlag(true);
					adminManager.modify_user(u);
				}
			}
		}

		return "userList_del_success";
	}
	
	
	

}
